package com.wash.shoes.domain.washer;


import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wash.shoes.common.config.MachineConfig;
import com.wash.shoes.entity.OrderPo;
import com.wash.shoes.entity.ShoePo;
import com.wash.shoes.entity.ShopPo;
import com.wash.shoes.service.OrderService;
import com.wash.shoes.service.ShoeService;
import com.wash.shoes.service.ShopService;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.*;

@Component
@Log4j2
public class WasherPostService {


    // 配置文件
    @Resource
    MachineConfig machineConfig;
    // 请求
    @Resource
    RestTemplate restTemplate;
    // 订单
    @Resource
    OrderService orderService;
    // 鞋子
    @Resource
    ShoeService shoeService;
    // 店铺
    @Resource
    ShopService shopService;

    //    @PostConstruct
    private void init() {
        log.info("====");

        WasherDto washerDto = new WasherDto();
        washerDto.setTiaoMa("W20250211xd0101286");
        washerDto.setOrdersNumber("O20250210xd0100593");
        washerDto.setYiWuNum(Integer.parseInt("3"));
        washerDto.setPeiJianNum(Integer.parseInt("0"));
        washerDto.setRuChangTime(DateUtil.formatTime(new Date()));
        washerDto.setIsPeiJian(false);


        WasherDto washerDto2 = new WasherDto();
        washerDto2.setTiaoMa("W20250211xd0101287");
        washerDto2.setOrdersNumber("O20250210xd0100593");
        washerDto2.setYiWuNum(Integer.parseInt("3"));
        washerDto2.setPeiJianNum(Integer.parseInt("0"));
        washerDto2.setRuChangTime(DateUtil.formatTime(new Date()));
        washerDto2.setIsPeiJian(false);

        sendToMachine(Arrays.asList(washerDto, washerDto2));

        log.info("===");
    }

    /**
     * 订单的切换
     *
     * @param orderPo
     */
    public void sendToMachine(OrderPo orderPo) {
        List<ShoePo> oderShoe = shoeService.getByOrderId(orderPo.getId());
        List<WasherDto> washerDtos = new ArrayList<>(oderShoe.size());
        for (ShoePo shoePo : oderShoe) {
            WasherDto washerDto = new WasherDto();
            washerDto.setTiaoMa(shoePo.getWashCode());
            washerDto.setOrdersNumber(orderPo.getOrderNo());
            washerDto.setYiWuNum(Integer.valueOf(String.valueOf(oderShoe.size())));
            washerDto.setPeiJianNum(Integer.valueOf(0));
            washerDto.setRuChangTime(DateUtil.formatTime(new Date()));
            washerDto.setIsPeiJian(false);
            washerDtos.add(washerDto);
        }
        sendToMachine(washerDtos);
    }

    /**
     * 推送信息
     *
     * @param dataList
     */
    public void sendToMachine(List<WasherDto> dataList) {
        //
        if (dataList == null || dataList.isEmpty()) {
            return;
        }

        String url = machineConfig.getUrl() + machineConfig.getQudaoid();
        HttpHeaders headers = new HttpHeaders();
        //头部类型
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization", machineConfig.getAuthorization());
        //构造实体对象
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.put("arrayParam", Collections.singletonList(dataList));
        HttpEntity<List<WasherDto>> request = new HttpEntity<>(dataList, headers);

        //发起请求,服务地址，请求参数，返回消息体的数据类型
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
        //body
        Object returnInfo = response.getBody();
        WashReturn washReturn = JSON.parseObject(returnInfo.toString(), WashReturn.class);
        // 如果成功就
        OrderPo orderPo = orderService.getByOrderNo(dataList.get(0).getOrdersNumber());
        ShopPo shopPo = shopService.getById(orderPo.getShopId());
        if (washReturn.isState()) {
            // 鞋子的信息更新
            for (WasherDto washerDto : dataList) {
                ShoePo shoePo = shoeService.shoeDetailByWashCode(washerDto.getTiaoMa());
                shoePo.setWashShop(orderPo.getWashFactory());
                shoeService.updateById(shoePo);
            }
            orderPo.setPushAutoFactory(1);
            orderService.updateById(orderPo);
        }else {
            orderPo.setPushAutoFactory(0);
            orderService.updateById(orderPo);
        }
        // 推送是被
        log.info("消息推送为 {} {}", JSONObject.toJSONString(dataList), JSONObject.toJSONString(returnInfo));

    }


}
